from django.shortcuts import render
from django.contrib.auth import authenticate, login
from django.shortcuts import render, get_object_or_404
from django.shortcuts import get_list_or_404
from django.http import HttpResponse
from django.template import RequestContext, loader
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse


# user lib
from django.contrib.auth.models import User
from django.contrib.auth import logout

from django.contrib.auth.decorators import login_required
from django.template.defaulttags import csrf_token
# Create your views here.
from main.models import News
from main.models import SchoolDisplay
from main.models import DynamicInfo
from main.models import PushInfos
import main

def load_template(template_path):
	return loader.get_template(template_path)

def get_news():
	return get_list_or_404(News)

def get_schoolPic():
	return get_list_or_404(SchoolDisplay)

def ifLogin(request):
	if request.user.is_active:
		return request.user.get_username()
	else:
		return None
def get_dynamic_info():

	return get_list_or_404(DynamicInfo)
def get_push_infos():
	return get_list_or_404(PushInfos)

def home(request):
	template = loader.get_template("base/main.html")
	if request.method == "GET":
		news = get_news()
		schoolPictures = get_schoolPic()
		infos = get_dynamic_info()
		push_infos = get_push_infos()
		counts = len(news)
		if request.user.is_active:
			user_name = request.user.get_username()
		else:
			user_name = None
		context = RequestContext(request,{
				'schoolPic':schoolPictures,
				'announcements':news,
				'counts':counts,
				'infos':infos,
				'pushinfos':push_infos,
				'login_user':user_name,
			})
		return HttpResponse(template.render(context))

	# if request.method == "POST":
	# 	return HttpResponseRedirect(reversed("main:news"))
############# user controller

def signView(request):
	if (request.method == "POST"):
		args = request.POST
		if 'name' in args:
			name = args['name']
		if 'pass' in args:
			password = args['pass']
		if 'email' in args:
			email = args['email']
		if name and password and email:
			new_user = User.objects.create_user(name,email,password)
			new_user.is_staff = True
			new_user.save()

		return HttpResponseRedirect(reversed('main:home'))

	return render(request,'main/sign.html')


def loginView(request):
	template = load_template("main/login.html")
	if request.method == "GET":
		
		if  request.user.is_active:
			return HttpResponseRedirect(reverse('main:home'))
		else:

			context = RequestContext(request)
			return HttpResponse(template.render(context))
	if request.method == "POST":
		args = request.POST
		if "name" in args :
			user_name = args['name']
		else :
			user_name = None
		if "password" in args:
			user_pass = args["password"]

		if user_pass and user_pass :
			this_user =  authenticate(username=user_name, password=user_pass)

			if  this_user:
				login(request,this_user)

			

			if this_user.is_active:
				return HttpResponseRedirect(reverse('main:home'))
			else:
				error_info ="login failed .. authenticate failed" 
				context = RequestContext(request,{
					'error_info':error_info,
				})		
				return  HttpResponse(template.render(context))
		else :
			error_info ="login failed .. authenticate failed"
			context = RequestContext(request,{
					'error_info':error_info,
				})
			return HttpResponse(template.render(context))

def logoutView(request):
	if request.user.is_active:
		logout(request)
		return HttpResponseRedirect(reverse("main:home"))
	else:
		return HttpResponseRedirect(reverse ("main:home"))

#############  views 
def newsView(request):
	template = load_template("main/newsView.html")
	if request.method == "GET":
		news = get_list_or_404(News)
		news_dealed = []
		for i in news:
			if len(i.title) >13:
				i.title = i.title[:10]+"..."
			
	
		map(lambda x: x.get_summary(),news)
		context = RequestContext(
				request, {
					'news_info':news,
					'login_user':ifLogin(request),
				}
			)

		return HttpResponse(template.render(context))


def detailNews(request,news_id):
	if request.method == "GET":
		template =load_template("main/detail.html")
		if news_id:
			news = get_object_or_404(News,pk=news_id)
			context = RequestContext(request,{
					'news_info':news,
					'login_user':ifLogin(request),
				})
			return HttpResponse(template.render(context))
		else :
			HttpResponseRedirect(reverse('main:error'),args=("no such html"))
		

def lab_views(request,name):

	template = load_template("main/{}.html".format(name))
	
	context = RequestContext(request)
	return HttpResponse(template.render(context))


def testView(request):
	template = load_template("main/test.html")
	if request.method == "GET":
		context = RequestContext(request,{
			'login_user':ifLogin(request),
		})
		return HttpResponse(template.render(context))


def errorView(request,info):
	template = load_template("main/error.html")
	context= RequestContext(request,{
			"error_info" :  info,
		})
	return HttpResponse(template.render(context))


